#include "xmlparse.h"

XmlParse::XmlParse(const string &xmlDoc, bool isFile /* = false */)
{
	if (isFile)
		xml.Load(MCD_STR(xmlDoc.c_str()));
	else
		xml.SetDoc(MCD_STR(xmlDoc.c_str()));
}

XmlParse::~XmlParse()
{
}

string XmlParse::ReadString(const string &root, const string &child, const string &attr, int idx /* = -1 */)
{
	xml.ResetPos();

	if (xml.FindElem(MCD_STR(root.c_str())))
	{

		if (idx < 0)
		{
			if (xml.FindChildElem(child.c_str()))
			{
				return xml.GetChildAttrib(attr.c_str());
			}
			else
				return "";
		}
		else
		{
			for (int i = 0; i <= idx; i++)
			{

				if (!xml.FindChildElem(child.c_str()))
					return "";

				if (i == idx)
					return xml.GetChildAttrib(attr.c_str());

			}
		}
	}

	return "";
}

int XmlParse::ReadInt(const string &root, const string &child, const string &attr, int idx /* = -1 */)
{
	const int iError = 0;
	xml.ResetPos();

	if (xml.FindElem(root.c_str()))
	{

		if (idx < 0)
		{
			if (xml.FindChildElem(child.c_str()))
			{
				return atoi(MCD_2PCSZ(xml.GetChildAttrib(attr.c_str())));
			}
			else
				return iError;
		}
		else
		{
			for (int i = 0; i <= idx; i++)
			{

				if (!xml.FindChildElem(child.c_str()))
					return iError;

				if (i == idx)
					return atoi(MCD_2PCSZ(xml.GetChildAttrib(attr.c_str())));

			}
		}
	}

	return iError;
}

long XmlParse::ReadLong(const string &root, const string &child, const string &attr, int idx /* = -1 */)
{
	const long iError = 0;
	xml.ResetPos();

	if (xml.FindElem(root.c_str()))
	{

		if (idx < 0)
		{
			if (xml.FindChildElem(child.c_str()))
			{
				return atol(MCD_2PCSZ(xml.GetChildAttrib(attr.c_str())));
			}
			else
				return iError;
		}
		else
		{
			for (int i = 0; i <= idx; i++)
			{

				if (!xml.FindChildElem(child.c_str()))
					return iError;

				if (i == idx)
					return atol(MCD_2PCSZ(xml.GetChildAttrib(attr.c_str())));

			}
		}
	}

	return iError;
}

double XmlParse::ReadDouble(const string &root, const string &child, const string &attr, int idx /* = -1 */)
{
	const double iError = 0.0;
	xml.ResetPos();

	if (xml.FindElem(root.c_str()))
	{

		if (idx < 0)
		{
			if (xml.FindChildElem(child.c_str()))
			{
				return atof(MCD_2PCSZ(xml.GetChildAttrib(attr.c_str())));
			}
			else
				return iError;
		}
		else
		{
			for (int i = 0; i <= idx; i++)
			{

				if (!xml.FindChildElem(child.c_str()))
					return iError;

				if (i == idx)
					return atof(MCD_2PCSZ(xml.GetChildAttrib(attr.c_str())));

			}
		}
	}

	return iError;
}

long long XmlParse::ReadInt64(const string &root, const string &child, const string &attr, int idx /* = -1 */)
{
	const long long iError = 0;
	xml.ResetPos();

	if (xml.FindElem(root.c_str()))
	{

		if (idx < 0)
		{
			if (xml.FindChildElem(child.c_str()))
			{
				return atoll(MCD_2PCSZ(xml.GetChildAttrib(attr.c_str())));
			}
			else
				return iError;
		}
		else
		{
			for (int i = 0; i <= idx; i++)
			{

				if (!xml.FindChildElem(child.c_str()))
					return iError;

				if (i == idx)
					return atoll(MCD_2PCSZ(xml.GetChildAttrib(attr.c_str())));

			}
		}
	}

	return iError;
}

